﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace T02cpsever
{
    class ThreadTcpSever
        
    {
        private Socket sever;
        public ThreadTcpSever() {
            //1.创建套接字
            //IP地址 测试使用本地地址
            IPAddress localAddress = IPAddress.Parse("127.0.0.1");
            IPEndPoint iep = new IPEndPoint(localAddress,13000);
            sever = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            //2.建立监听
            //绑定相应的IP:port
            sever.Bind(iep);
            //监听
            sever.Listen(20);
            Console.WriteLine("等待客户机连接......");
            while (true)
            {
                Socket client = sever.Accept();
                //创建工作线程
                ClientThread newClient = new ClientThread(client);
                Thread newThread = new Thread(new ThreadStart(newClient.handleDate));
                //启动线程
                newThread.Start();
            }
                }
        static void Main(string[] args)
        {
            //生成服务实例
            ThreadTcpSever instance = new ThreadTcpSever();
        }
    }
}
